unit Serialset;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls, 
  Buttons, ExtCtrls, RzButton, SPComm;

type
  TSerialsetfrm = class(TForm)
    GroupBox1: TGroupBox;
    Label13: TLabel;
    comPort: TComboBox;
    GroupBox2: TGroupBox;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    comParity: TComboBox;
    comBtSz: TComboBox;
    ComStpBits: TComboBox;
    GroupBox3: TGroupBox;
    Label17: TLabel;
    comBdRt: TComboBox;
    GroupBox4: TGroupBox;
    Label18: TLabel;
    Label19: TLabel;
    RcvMemo: TMemo;
    txtSend: TEdit;
    ckAtoDA: TCheckBox;
    RzButton1: TRzButton;
    RzBitBtn5: TRzBitBtn;
    RzBitBtn1: TRzBitBtn;
    SerialTest: TComm;
    CheckBox1: TCheckBox;
    procedure RzBitBtn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure RzButton1Click(Sender: TObject);
    procedure SerialTestReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    procedure RzBitBtn5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    RcvData:String;
    //StartSerialPort:Boolean;
    procedure SendData(data:string);
    procedure IniSerialPort;
  end;

var
  Serialsetfrm: TSerialsetfrm;

implementation

{$R *.dfm}

uses
  includefile;

procedure TSerialsetfrm.IniSerialPort;
begin
  with SerialTest do begin
    StopComm;
    CommName:=Trim(comPort.Text);
    BaudRate:=BDRT[comBdRt.ItemIndex];
    Parity:=PRTY[comParity.ItemIndex];
    ByteSize:=BTSZ[comBtSz.ItemIndex];
    StopBits:=STPBT[comStpBits.ItemIndex];
    try
      StartComm;
    except
      Application.MessageBox(Pchar(GetCaption('MsgBox',108)),
                             Pchar(GetCaption('MsgBox',109)),16);
    end;
  end;
end;

procedure TSerialsetfrm.SendData(data:string);
var
  temp:string;
  SdBuff:PChar;
begin
  temp:=data;
  SdBuff:=pchar(temp);
  SerialTest.WriteCommData(sdBuff,Length(temp));
end;

procedure TSerialsetfrm.RzBitBtn1Click(Sender: TObject);
begin
  SerialTest.StopComm;
  if CheckBox1.Checked then begin
    with inif do begin
      WriteString('system','SerialPort',comPort.Text);
      Writeinteger('system','BaudRate',comBdRt.ItemIndex);
      Writeinteger('system','Parity',comParity.ItemIndex);
      Writeinteger('system','ByteSize',comBtsz.ItemIndex);
      Writeinteger('system','StopBits',comStpBits.ItemIndex);
    end;
  end;
  ModalResult:=MrOk;
end;

procedure TSerialsetfrm.FormCreate(Sender: TObject);
var
  I:integer;
begin
  for I:=0 to ComponentCount-1 do begin
    if Components[I] is TLabel then begin
      if 0<>TLabel(Components[I]).Tag then
        TLabel(Components[I]).Caption:=
                GetCaption('Serialsetfrm',TLabel(Components[I]).Tag);
    end;
    if Components[I] is TRzButton then begin
      if 0<>TRzButton(Components[I]).Tag then
        TRzButton(Components[I]).Caption:=
                GetCaption('Serialsetfrm',TRzButton(Components[I]).Tag);
    end;
    if Components[I] is TCheckBox then begin
      if 0<>TCheckBox(Components[I]).Tag then
        TCheckBox(Components[I]).Caption:=
                GetCaption('Serialsetfrm',TCheckBox(Components[I]).Tag);
    end;
    if Components[I] is TGroupBox then begin
      if 0<>TGroupBox(Components[I]).Tag then
        TGroupBox(Components[I]).Caption:=
                GetCaption('Serialsetfrm',TGroupBox(Components[I]).Tag);
    end;
  end;
  //StartSerialPort:=False;
end;

procedure TSerialsetfrm.RzButton1Click(Sender: TObject);
var
  tmp:string;
begin
  tmp:=Trim(txtSend.Text);
  if ''=tmp then exit;
  if ckAtoDa.Checked then tmp:=tmp+#13+#10;
  SendData(tmp);
end;

procedure TSerialsetfrm.SerialTestReceiveData(Sender: TObject;
  Buffer: Pointer; BufferLength: Word);
begin
  SetLength(RcvData,BufferLength);
  CopyMemory(@RcvData[1],Buffer,BufferLength);
  RcvMemo.Text:=RcvMemo.Text+RcvData;
  RcvMemo.SelLength:=Length(RcvMemo.Text);
end;

procedure TSerialsetfrm.RzBitBtn5Click(Sender: TObject);
begin
  IniSerialPort;
end;

end.
